package cn.com.recode.a开始案例;

import java.util.Enumeration;
import java.util.Vector;

public class Customer {

    private String _name;

    private Vector _rentals = new Vector();

    public Customer(String name) {
        this._name = name;
    }

    public void addRental(Rental rental) {
        _rentals.add(rental);
    }

    public String getName() {
        return _name;
    }

    /**
     * 1.太长的代码不易扩展，若修改则会很麻烦
     * 2.缺乏对持续性扩展考虑
     * 3.需要保证代码一定程度上的服用来减少相同逻辑代码的出现
     * 4.建立可靠的测试环境减少BUG的出现，好的测试是重构的根本
     * @return
     */
    public String statement() {
        double totalAmout = 0;
        int frenquentRenterPoints = 0;
        Enumeration rentals = _rentals.elements();
        String result = "Rental  Record for " + getName() + "\n";
        while (rentals.hasMoreElements()) {
            double thisAmout = 0;
            Rental each = (Rental) rentals.nextElement();

            switch (each.getMovie().getPriceCode()) {
                case Movie.REGULAR:
                    thisAmout += 2;
                    if (each.getDaysRented() > 2) {
                        thisAmout += (each.getDaysRented() - 2) * 1.5;
                    }
                    break;
                case Movie.NEW_RELEASE:
                    thisAmout += each.getDaysRented() * 3;
                    break;
                case Movie.CHILDRENS:
                    thisAmout += 1.5;
                    if (each.getDaysRented() > 3) {
                        thisAmout += (each.getDaysRented() - 3) * 1.5;
                    }
                    break;
            }

            frenquentRenterPoints ++;
            if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) {
                frenquentRenterPoints ++;
            }

            result += "\t" +each.getMovie().getTitle()+"\t"+
                    String.valueOf(thisAmout) + "\n";

            totalAmout += thisAmout;
        }

        result += "Amout owed is "+String.valueOf(totalAmout) +"\n";
        result += "You earned " + String.valueOf(frenquentRenterPoints)+" frequent renter points";
        return result;
    }
}
